package com.learn.excel.core.util.shell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;

/**
 * @Classname CommandStreamGobbler
 * @Description TODO
 * @Author zxy
 */
public class CommandStreamGobbler extends Thread {

	private InputStream is;

	private String command;

	private boolean readFinish = false;

	private boolean ready = false;

	// 命令执行结果，0:执行中，1:超时，2：执行完成
	private int commandResult = 0;

	public List<String> infoList = new LinkedList<String>();

	public CommandStreamGobbler(InputStream is, String command) {
		this.is = is;
		this.command = command;
	}

	public void run() {

		InputStreamReader isr = null;
		BufferedReader br = null;
		String OSName = System.getProperties().getProperty("os.name");
		try {
			if (OSName.contains("Windows")) {
				isr = new InputStreamReader(is, Charset.forName("GBK"));
			}
			else {
				isr = new InputStreamReader(is);
			}
			br = new BufferedReader(isr);
			String line = null;
			ready = true;
			while (commandResult != 1) {
				if (br.ready() || commandResult == 2) {
					if ((line = br.readLine()) != null) {
						infoList.add(line);
					}
					else {
						break;
					}
				}
				else {
					Thread.sleep(100);
				}
			}
		}
		catch (IOException | InterruptedException ioe) {
			System.out.println("正在执行的命令：" + command + "有IO异常！");
		}
		finally {
			try {
				if (br != null) {
					br.close();
				}
				if (isr != null) {
					isr.close();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
				System.out.println("close IO,正在执行的命令：" + command + "有IO异常！");
			}
			readFinish = true;
		}

	}

	public InputStream getIs() {
		return is;
	}

	public String getCommand() {
		return command;
	}

	public boolean isReadFinish() {
		return readFinish;
	}

	public boolean isReady() {
		return ready;
	}

	public List<String> getInfoList() {
		return infoList;
	}

	public void setTimeout(int timeout) {
		this.commandResult = timeout;
	}

}
